package com.db_tenant;

import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory;
import io.ebean.config.ServerConfig;
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
import lombok.extern.slf4j.Slf4j;
import org.avaje.datasource.DataSourceConfig;
import org.avaje.datasource.DataSourcePool;
import org.avaje.datasource.core.Factory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Profile;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Configuration
@Profile("dev")
public class CitusRwTenantConfig_Dev extends CitusRwTenantConfig {

    String coordinate = "127.0.0.1";
    List<String> workers = Arrays.asList("127.0.0.1", "127.0.0.1");
    String port = "5432";
    String database = "dev_imessage";
    String username = "postgres";
    String password = "123456";

    //----------------
    @Bean
    @Lazy
    public EbeanServer ebeanServer() {
        System.out.println("load dev pconfiguration.");
        String jdbcUrl = String.format("jdbc:postgresql://%s:%s/%s", coordinate(), port(), database());
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDriver("org.postgresql.Driver");
        dsConfig.setUrl(jdbcUrl);
        dsConfig.setUsername(username());
        dsConfig.setPassword(password());
        DataSourcePool ds = new Factory().createPool("dev_localhost", dsConfig);

        ServerConfig config = new ServerConfig();
        config.setDefaultServer(true);
        config.setName("dev_localhost");
        config.setDatabasePlatform(new PostgresPlatform());
        config.setDdlGenerate(true);
     //     config.setDdlRun(true);//**重置数据库
        config.setDataSource(ds);
        return EbeanServerFactory.create(config);
    }

    //----------------
    @Override
    public String coordinate() {
        return coordinate;
    }

    @Override
    public List<String> workers() {
        return workers;
    }

    @Override
    public String port() {
        return port;
    }

    @Override
    public String database() {
        return database;
    }

    @Override
    public String username() {
        return username;
    }

    @Override
    public String password() {
        return password;
    }
}
